﻿using Microsoft.EntityFrameworkCore;
using XsqTech.SmartOA.Domain.Table;
using XsqTech.SmartOA.Dtos.DTO.Dict;
using XsqTech.SmartOA.EFCore;
using XsqTech.SmartOA.EFCore.Respository;
using XsqTech.SmartOA.IService;

namespace XsqTech.SmartOA.Service
{
    public class DictDetailsService : IDictDetailsService
    {
        IDictDetailsService _basedict;
        readonly IUnitOfWork _unitOfWork;
        public MyDbContext _db;

        public DictDetailsService(IDictDetailsService basedict, IUnitOfWork unitOfWork, MyDbContext db)
        {
            _basedict = basedict;
            _unitOfWork = unitOfWork;
            _db = db;
        }
        /// <summary>
        /// 数据字典从表显示分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="size"></param>
        /// <param name="dang"></param>
        /// <returns></returns>
        public List<DictDetails> DictDetailsShow(string name, int id = 0, int size = 3, int dang = 1)
        {
            var list = _db.DictDetails.Where(x => x.DetailsState == 1).ToList();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x => x.DetailsName.Contains(name)).ToList();
            }
            if (id != 0)
            {
                list = list.Where(x => x.Dictid == id).ToList();
            }
            return list;
        }
        /// <summary>
        /// 数据字典从表添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DictDetailsAdd(DictDetails model)
        {
            _basedict.DictDetailsAdd(model);
            return _unitOfWork.SaveChangs();
        }
        /// <summary>
        /// 数据字典从表修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DictDetailsUpdate(DictDetailsUpdateDTO model)
        {
            _db.Database.ExecuteSqlRaw($"UPDATE dictDetails set DetailsName='{model.DetailsName}',Dictid='{model.Dictid}',DetailsUpdateTime='{model.DetailsUpdateTime}',DetailsUpdateUser='{model.DetailsUpdateUser}',DetailsState={model.DetailsState},DetailsCode='{model.DetailsCode}'where DetailsId={model.DetailsId}");
            return _unitOfWork.SaveChangs();
        }
        /// <summary>
        /// 数据字典从表删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DetailsDelete(int id)
        {
            _basedict.DetailsDelete(id);
            return _unitOfWork.SaveChangs();
        }
        /// <summary>
        /// 根据主键获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DictDetails DictRefill(int id)
        {
            return _basedict.DictRefill(id);
        }
    }
}